﻿Option Explicit On
Imports System.IO
Imports System.Net
''' <summary>
''' Clase que obtiene datos del piloto del archivo de VATSIM de pilotos online
''' Devuelve getDatos Array con los 41 campos de datos
''' </summary>
''' <remarks></remarks>
Public Class VatsimDatosPiloto
    Private datos(41) As String
    Public Sub New()
        ConectaVatsim()
    End Sub
    Public Sub New(ByVal codigo As String)
        cnfVATSIM = codigo
        ConectaVatsim()
    End Sub
    ''' <summary>
    ''' Contructor copia de la clase
    ''' </summary>
    ''' <param name="copia"></param>
    ''' <remarks></remarks>
    Public Sub New(ByRef copia As VatsimDatosPiloto)
        Me.datos = copia.getDatos
    End Sub

    Public ReadOnly Property getDatos() As String()
        Get
            Return Me.datos
        End Get
    End Property

    Private Sub ConectaVatsim()
        Dim URLString As String = "http://www.net-flyer.net/DataFeed/vatsim-data.txt"

        Dim inStream As StreamReader
        Dim webRequest As WebRequest
        Dim webresponse As WebResponse
        Dim line As String
        Dim campos() As String
        Dim campo As String
        Try
            webRequest = webRequest.Create(URLString)
            webresponse = webRequest.GetResponse()
            inStream = New StreamReader(webresponse.GetResponseStream())
            While inStream.EndOfStream = False
                line = inStream.ReadLine()
                'Debug.Write(inStream.ReadToEnd())
                'Debug.Write(line)
                If line.Length >= 9 Then
                    If line.Substring(0, 9) = "!CLIENTS:" Then
                        Exit While
                    End If

                End If

            End While
            'Quitar en release
            '>>>
            'cnfVATSIM = "1150037"
            '<<<
            '0 callsign:  1 cid: 2 realname: 3 clienttype: 4 frequency:
            '5 latitude:  6 longitude: 7 altitude: 8 groundSpeed: 9 planned_aircraft: 10 planned_tascruise:
            '11 planned_depairport: 12 planned_altitude: 13 planned_destairport: 14 server: 15 protrevision:
            '16 rating: 17 transponder: 18 facilitytype: 19 visualrange: 20 planned_revision: 21 planned_flighttype: 
            '22 planned_deptime: 23 planned_actdeptime: 24 planned_hrsenroute: 25 planned_minenroute: 
            '26 planned_hrsfuel: 27 planned_minfuel: 28 planned_altairport: 29 planned_remarks: 30 planned_route:
            '31 planned_depairport_lat: 32 planned_depairport_lon: 33 planned_destairport_lat:
            '34 planned_destairport_lon: 35 atis_message: 36 time_last_atis_received: 37 time_logon: 38 heading:
            '39 QNH_iHg: 40 QNH_Mb
            campos = inStream.ReadLine().Split(New Char() {":"c})


            Do
                campo = campos(1)
                If campo = cnfVATSIM Then
                    Exit Do
                End If
                campos = inStream.ReadLine().Split(New Char() {":"c})
            Loop While campos.Length = 42
            If campos.Length = 42 Then
                For i = 0 To 41
                    Me.datos(i) = campos(i)
                Next

            Else
                Me.datos(0) = Nothing
            End If
            inStream.Close()
            '***********************
            '***********************

        Catch ex As WebException
            'MessageBox.Show("Ha habido un problema al conectar con la URL de VATSIM", "Error de conexión VATSIM", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly)




        End Try
    End Sub
End Class
